package com.road.yishi.execaction;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 抽象动作，每个动作加入队列里，一條一條執行
 * 
 * @author jackson.zhang
 *
 */

public abstract class Action implements Runnable {

	protected final static Logger LOG = LoggerFactory.getLogger(Action.class);
	/**
	 * 动作队列
	 */
	private ActionQueue queue;
	/**
	 * 当前动作创建的时间
	 */
	protected Long createTime;

	public Action(ActionQueue queue) {
		this.queue = queue;
		createTime = System.currentTimeMillis();
	}

	public ActionQueue getQueue() {
		return queue;
	}

	/**
	 * 加上final使子类不能覆写该方法
	 */
	@Override
	public final void run() {
		if (queue != null) {
			long start = System.currentTimeMillis();
			try {
				execute();
				long end = System.currentTimeMillis();
				long interval = end - start;
				long leftTime = end - createTime;
				if (interval >= 1000) {
					LOG.warn("execute action : " + this.toString() + ", interval : " + interval + ", leftTime : "
							+ leftTime + ", size : " + queue.getQueue().size());
				}
			} catch (Exception e) {
				LOG.error("run action execute exception. action : " + this.toString(), e);
			} finally {
				queue.dequeue(this);
			}
		}
	}

	/**
	 * 
	 * @Name: execute
	 * @Description:动作的逻辑
	 * @Author: WeiHui.Zhang
	 * @Version: V1.00
	 * @Date: 2016年2月1日下午12:08:31
	 * @Return: void
	 */
	public abstract void execute();
}
